<template>
  <div class="product-list-container">
    <div class="page-header">
      <h2>商品列表</h2>
      <el-button type="primary" @click="goToPublish">发布商品</el-button>
    </div>

    <div class="filter-section">
      <el-card>
        <div class="filter-form">
          <el-form :inline="true" :model="searchForm">
            <el-form-item label="搜索">
              <el-input
                v-model="searchForm.keyword"
                placeholder="商品名称或描述"
                prefix-icon="el-icon-search"
                @keyup.enter="handleSearch"
              />
            </el-form-item>
            <el-form-item label="分类">
              <el-select
                v-model="searchForm.categoryId"
                placeholder="选择分类"
                clearable
              >
                <el-option
                  v-for="category in mockCategories"
                  :key="category.id"
                  :label="category.name"
                  :value="category.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="价格范围">
              <el-input-number
                v-model="searchForm.minPrice"
                placeholder="最低价"
                :min="0"
                style="width: 120px"
              />
              <span style="margin: 0 10px">-</span>
              <el-input-number
                v-model="searchForm.maxPrice"
                placeholder="最高价"
                :min="0"
                style="width: 120px"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleSearch">搜索</el-button>
              <el-button @click="reset">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>

    <div class="sort-section">
      <span>排序方式：</span>
      <el-radio-group v-model="sortBy" @change="handleSort">
        <el-radio-button label="default">默认排序</el-radio-button>
        <el-radio-button label="price_asc">价格从低到高</el-radio-button>
        <el-radio-button label="price_desc">价格从高到低</el-radio-button>
        <el-radio-button label="newest">最新发布</el-radio-button>
      </el-radio-group>
    </div>

    <div class="product-list">
      <div class="product-grid">
        <div v-for="product in filteredProducts" :key="product.id" class="product-card">
          <router-link :to="`/product/${product.id}`">
            <div class="product-image">
              <img :src="product.image" alt="商品图片">
            </div>
            <div class="product-info">
              <h3 class="product-title">{{ product.title }}</h3>
              <div class="product-price">¥{{ product.price }}</div>
              <div class="product-meta">
                <span>{{ product.categoryName }}</span>
                <span>{{ product.createdAt }}</span>
              </div>
              <div class="product-seller">
                <el-avatar :size="24" :src="product.sellerAvatar || 'https://via.placeholder.com/40'" />
                <span>{{ product.sellerName }}</span>
              </div>
            </div>
          </router-link>
        </div>
      </div>

      <!-- 空状态 -->
      <div v-if="filteredProducts.length === 0" class="empty-state">
        <el-empty description="暂无商品" />
      </div>
    </div>

    <!-- 分页 -->
    <div class="pagination" v-if="filteredProducts.length > 0">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[12, 24, 36, 48]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="filteredProducts.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useProductStore } from '../../store'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()
const productStore = useProductStore()

const searchForm = reactive({
  keyword: '',
  categoryId: '',
  minPrice: null,
  maxPrice: null
})

const sortBy = ref('default')
const currentPage = ref(1)
const pageSize = ref(12)

// 组件挂载时检查URL参数
onMounted(() => {
  // 从URL参数中获取搜索关键词
  if (route.query.keyword) {
    searchForm.keyword = route.query.keyword
    handleSearch()
  }
  
  // 从URL参数中获取分类ID
  if (route.query.categoryId) {
    searchForm.categoryId = route.query.categoryId
    handleSearch()
  }
  
  // 检查是否只显示我的商品
  if (route.query.my === '1') {
    // 这里可以添加只显示当前用户商品的逻辑
    ElMessage.info('切换到我的商品列表')
  }
})

// 模拟分类数据
const mockCategories = [
  { id: 1, name: '数码产品' },
  { id: 2, name: '图书文具' },
  { id: 3, name: '服装鞋包' },
  { id: 4, name: '生活用品' },
  { id: 5, name: '运动器材' },
  { id: 6, name: '其他' }
]

// 模拟商品数据
const mockProducts = [
  {
    id: 1,
    title: 'iPhone 13 128GB 蓝色 95新',
    price: 3999,
    image: 'https://via.placeholder.com/300x300',
    categoryId: 1,
    categoryName: '数码产品',
    sellerName: '小明',
    sellerAvatar: 'https://via.placeholder.com/40',
    createdAt: '2024-10-20'
  },
  {
    id: 2,
    title: '《算法导论》第三版 全新',
    price: 89,
    image: 'https://via.placeholder.com/300x300',
    categoryId: 2,
    categoryName: '图书文具',
    sellerName: '小红',
    sellerAvatar: 'https://via.placeholder.com/40',
    createdAt: '2024-10-21'
  },
  {
    id: 3,
    title: 'Nike Air Force 1 白鞋 42码',
    price: 350,
    image: 'https://via.placeholder.com/300x300',
    categoryId: 3,
    categoryName: '服装鞋包',
    sellerName: '小李',
    sellerAvatar: 'https://via.placeholder.com/40',
    createdAt: '2024-10-19'
  },
  {
    id: 4,
    title: '小米空气净化器 2S',
    price: 299,
    image: 'https://via.placeholder.com/300x300',
    categoryId: 4,
    categoryName: '生活用品',
    sellerName: '小张',
    sellerAvatar: 'https://via.placeholder.com/40',
    createdAt: '2024-10-18'
  },
  {
    id: 5,
    title: '哑铃套装 20kg',
    price: 150,
    image: 'https://via.placeholder.com/300x300',
    categoryId: 5,
    categoryName: '运动器材',
    sellerName: '小王',
    sellerAvatar: 'https://via.placeholder.com/40',
    createdAt: '2024-10-17'
  },
  {
    id: 6,
    title: '索尼耳机 WH-1000XM4',
    price: 1299,
    image: 'https://via.placeholder.com/300x300',
    categoryId: 1,
    categoryName: '数码产品',
    sellerName: '小刘',
    sellerAvatar: 'https://via.placeholder.com/40',
    createdAt: '2024-10-16'
  },
  {
    id: 7,
    title: 'MacBook Pro 2022 14寸',
    price: 12999,
    image: 'https://via.placeholder.com/300x300',
    categoryId: 1,
    categoryName: '数码产品',
    sellerName: '小陈',
    sellerAvatar: 'https://via.placeholder.com/40',
    createdAt: '2024-10-15'
  },
  {
    id: 8,
    title: '考研英语词汇书',
    price: 39,
    image: 'https://via.placeholder.com/300x300',
    categoryId: 2,
    categoryName: '图书文具',
    sellerName: '小林',
    sellerAvatar: 'https://via.placeholder.com/40',
    createdAt: '2024-10-14'
  },
  {
    id: 9,
    title: '安踏运动套装',
    price: 259,
    image: 'https://via.placeholder.com/300x300',
    categoryId: 3,
    categoryName: '服装鞋包',
    sellerName: '小周',
    sellerAvatar: 'https://via.placeholder.com/40',
    createdAt: '2024-10-13'
  },
  {
    id: 10,
    title: '电热水壶 不锈钢',
    price: 89,
    image: 'https://via.placeholder.com/300x300',
    categoryId: 4,
    categoryName: '生活用品',
    sellerName: '小吴',
    sellerAvatar: 'https://via.placeholder.com/40',
    createdAt: '2024-10-12'
  },
  {
    id: 11,
    title: '瑜伽垫 加厚加宽',
    price: 59,
    image: 'https://via.placeholder.com/300x300',
    categoryId: 5,
    categoryName: '运动器材',
    sellerName: '小郑',
    sellerAvatar: 'https://via.placeholder.com/40',
    createdAt: '2024-10-11'
  },
  {
    id: 12,
    title: 'U盘 128GB 高速',
    price: 69,
    image: 'https://via.placeholder.com/300x300',
    categoryId: 1,
    categoryName: '数码产品',
    sellerName: '小冯',
    sellerAvatar: 'https://via.placeholder.com/40',
    createdAt: '2024-10-10'
  }
]

// 过滤和排序后的商品
const filteredProducts = computed(() => {
  let products = [...mockProducts]
  
  // 过滤
  if (searchForm.keyword) {
    products = products.filter(p => 
      p.title.toLowerCase().includes(searchForm.keyword.toLowerCase()) ||
      p.categoryName.toLowerCase().includes(searchForm.keyword.toLowerCase())
    )
  }
  
  if (searchForm.categoryId) {
    products = products.filter(p => p.categoryId === parseInt(searchForm.categoryId))
  }
  
  if (searchForm.minPrice !== null) {
    products = products.filter(p => p.price >= searchForm.minPrice)
  }
  
  if (searchForm.maxPrice !== null) {
    products = products.filter(p => p.price <= searchForm.maxPrice)
  }
  
  // 排序
  switch (sortBy.value) {
    case 'price_asc':
      products.sort((a, b) => a.price - b.price)
      break
    case 'price_desc':
      products.sort((a, b) => b.price - a.price)
      break
    case 'newest':
      products.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
      break
  }
  
  return products
})

const goToPublish = () => {
  router.push('/product/publish')
}

// 搜索功能
const handleSearch = () => {
  currentPage.value = 1 // 重置到第一页
  // 这里可以添加实际的搜索API调用
  ElMessage.success('搜索成功')
}

// 重置搜索条件
const reset = () => {
  searchForm.keyword = ''
  searchForm.categoryId = ''
  searchForm.minPrice = null
  searchForm.maxPrice = null
  sortBy.value = 'default'
  currentPage.value = 1
}

const handleSort = () => {
  currentPage.value = 1
}

const handleSizeChange = (newSize) => {
  pageSize.value = newSize
  currentPage.value = 1
}

const handleCurrentChange = (newPage) => {
  currentPage.value = newPage
}
</script>

<style scoped>
.product-list-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.page-header h2 {
  font-size: 32px;
  color: #333;
  position: relative;
  padding-left: 20px;
}

.page-header h2::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 8px;
  height: 30px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 4px;
}

.filter-section {
  background-color: #fff;
  padding: 25px;
  border-radius: 16px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
  margin-bottom: 30px;
  transition: all 0.3s ease;
}

.filter-section:hover {
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}

.filter-form {
  width: 100%;
}

.sort-section {
  margin-bottom: 30px;
  padding: 15px 0;
  border-bottom: 1px solid #e0e0e0;
}

.sort-section span {
  margin-right: 15px;
  font-size: 15px;
  color: #333;
  font-weight: 500;
}

/* 覆盖Element Plus组件样式 */
:deep(.el-select) {
  width: 150px;
}

:deep(.el-select .el-input__wrapper) {
  border-radius: 10px;
  transition: all 0.3s ease;
}

:deep(.el-select .el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px rgba(102, 126, 234, 0.2);
  border-color: #667eea;
}

:deep(.el-select .el-input.is-focus .el-input__wrapper) {
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
  border-color: #667eea;
}

:deep(.el-button) {
  border-radius: 10px;
  padding: 8px 20px;
  font-weight: 500;
  transition: all 0.3s ease;
}

:deep(.el-button--primary) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

:deep(.el-button--primary:hover) {
  background: linear-gradient(135deg, #5a67d8 0%, #6b46c1 100%);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

:deep(.el-radio-button__orig-radio:checked + .el-radio-button__inner) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-color: #667eea;
}

.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 30px;
  margin-bottom: 30px;
}

.product-card {
  background-color: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  position: relative;
  border: none;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.product-card:hover {
  transform: translateY(-10px);
  box-shadow: 0 15px 30px rgba(102, 126, 234, 0.15);
}

.product-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.product-card:hover::before {
  opacity: 1;
}

.product-image {
  width: 100%;
  height: 220px;
  overflow: hidden;
  position: relative;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.product-card:hover .product-image img {
  transform: scale(1.05);
}

.product-info {
  padding: 20px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.product-title {
  font-size: 18px;
  margin-bottom: 15px;
  color: #333;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.4;
  flex: 1;
}

.product-price {
  font-size: 24px;
  font-weight: bold;
  color: #ff4d4f;
  margin-bottom: 12px;
  display: inline-block;
}

.product-meta {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  color: #999;
  margin-bottom: 12px;
}

.product-seller {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #666;
  padding-top: 12px;
  border-top: 1px solid #f0f0f0;
}

.product-seller span {
  margin-left: 8px;
}

.empty-state {
  padding: 60px 0;
  text-align: center;
}

.no-results h3 {
  font-size: 24px;
  margin-bottom: 15px;
  color: #333;
}

.no-results p {
  font-size: 16px;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}

:deep(.el-pagination__item.is-active) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-color: transparent;
  color: white;
}

:deep(.el-pagination__item:hover:not(.is-disabled)) {
  color: #667eea;
  border-color: #667eea;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  :deep(.el-select) {
    width: 140px;
  }
}

@media (max-width: 768px) {
  .product-list-container {
    padding: 20px 15px;
  }
  
  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }
  
  .page-header h2 {
    font-size: 28px;
    margin-bottom: 0;
  }
  
  .filter-form .el-form--inline .el-form-item {
    margin-right: 0;
    margin-bottom: 15px;
  }
  
  :deep(.el-select) {
    width: 100%;
  }
  
  .product-grid {
    gap: 20px;
  }
  
  .product-image {
    height: 200px;
  }
}

@media (max-width: 480px) {
  .product-grid {
    grid-template-columns: 1fr;
  }
  
  .page-header h2 {
    font-size: 24px;
  }
}
</style>